- Coupon Add/Edit/Delete rule AJAX
This commit is contained in:
gmorel
2013-09-11 09:48:33 +02:00
parent 8be785e9b3
commit 7e2f57e618
2 changed files with 31 additions and 33 deletions

View File

@@ -41,8 +41,6 @@
$(function($){ $(function($){
miniBrowser(0, '/test_to_remove/datas_coupon_edit.json'); miniBrowser(0, '/test_to_remove/datas_coupon_edit.json');
Array.prototype.clean = function(deleteValue) { Array.prototype.clean = function(deleteValue) {
for (var i = 0; i < this.length; i++) { for (var i = 0; i < this.length; i++) {
if (this[i] == deleteValue) { if (this[i] == deleteValue) {
@@ -53,7 +51,6 @@
return this; return this;
}; };
// Init Rules // Init Rules
var initRules = function() { var initRules = function() {
var rules = []; var rules = [];
@@ -75,8 +72,6 @@
// Save Rules AJAX // Save Rules AJAX
var saveRuleAjax = function() { var saveRuleAjax = function() {
console.log('rulesToSave');
console.log(rulesToSave);
var $url = '{$urlAjaxUpdateRules}'; var $url = '{$urlAjaxUpdateRules}';
$.ajax({ $.ajax({
type: "POST", type: "POST",
@@ -107,12 +102,14 @@
// Add 1 Rule / or update the temporary Rules array then Save Rules via AJAX // Add 1 Rule / or update the temporary Rules array then Save Rules via AJAX
var addRuleAjax = function(id) { var addRuleAjax = function(id) {
// If update
if(typeof id === 'number' && id % 1 == 0) { if(typeof id === 'number' && id % 1 == 0) {
rulesToSave[id] = ruleToSave; rulesToSave[id] = ruleToSave;
} else { } else { // If create
rulesToSave.push(ruleToSave); rulesToSave.push(ruleToSave);
} }
// Save
saveRuleAjax(); saveRuleAjax();
} }
@@ -193,12 +190,15 @@
} }
onClickDeleteRule(); onClickDeleteRule();
// Remove rule on click // Update rule on click
var onClickUpdateRule = function() { var onClickUpdateRule = function() {
$('.constraint-update-btn').on('click', function (e) { $('.constraint-update-btn').on('click', function (e) {
e.preventDefault(); e.preventDefault();
var $this = $(this); var $this = $(this);
updateRuleAjax($this.attr('data-int')); updateRuleAjax($this.attr('data-int'));
// Hide row being updated
$this.parent().parent().remove();
}); });
} }
onClickUpdateRule(); onClickUpdateRule();
@@ -215,21 +215,23 @@
// Reload rule inputs when changing effect // Reload rule inputs when changing effect
var onRuleChange = function() { var onRuleChange = function() {
$('#category-rule').on('change', function (e) { $('#category-rule').on('change', function (e) {
loadRuleInputs($(this).val(), null, function(ruleToSave) {}); loadRuleInputs($(this).val(), null, function(ruleToSave) {literal}{}{/literal});
}); });
} }
onRuleChange(); onRuleChange();
// Fill in ready to be saved rule array
// var onInputsChange = function()
// In AJAX response
// Rule to save
var ruleToSave = {literal}{}{/literal};
// Rules which will be saved // Rules which will be saved
var rulesToSave = initRules(); var rulesToSave = initRules();
}); });
// Rule to save
var ruleToSave = {literal}{}{/literal};
</script> </script>
{/block} {/block}

View File

@@ -72,33 +72,29 @@
<script> <script>
// Init Rules to set // Init Rules to set
var ruleToSave = {};
ruleToSave['serviceId'] = '{$ruleId}'; ruleToSave['serviceId'] = '{$ruleId}';
ruleToSave['operators'] = {}; ruleToSave['operators'] = {literal}{}{/literal};
ruleToSave['values'] = {}; ruleToSave['values'] = {literal}{}{/literal};
{foreach from=$inputs.inputs key=name item=input} {foreach from=$inputs.inputs key=name item=input}
ruleToSave['operators']['{$name nofilter}'] = '{foreach from=$inputs.inputs[$name].availableOperators key=keyOperator item=valueOperator name=operators}{if $smarty.foreach.operators.first}{$keyOperator nofilter}{/if}{/foreach}'; ruleToSave['operators']['{$name nofilter}'] = '{foreach from=$inputs.inputs[$name].availableOperators key=keyOperator item=valueOperator name=operators}{if $smarty.foreach.operators.first}{$keyOperator nofilter}{/if}{/foreach}';
ruleToSave['values']['{$name nofilter}'] = '{if count($inputs.inputs[$name].availableValues) != 0}{foreach from=$inputs.inputs[$name].availableValues key=keyValue item=valueValue name=values}{if $smarty.foreach.values.first}{$keyValue nofilter}{/if}{/foreach}{else}to set{/if}'; ruleToSave['values']['{$name nofilter}'] = '{if count($inputs.inputs[$name].availableValues) != 0}{foreach from=$inputs.inputs[$name].availableValues key=keyValue item=valueValue name=values}{if $smarty.foreach.values.first}{$keyValue nofilter}{/if}{/foreach}{else}to set{/if}';
{/foreach} {/foreach}
// Fill in ready to be saved rule array // Fill in ready to be saved rule array
var onInputsChange = function() {literal}{{/literal} var onInputsChange = function() {literal}{{/literal}
{foreach from=$inputs.inputs key=name item=input} {foreach from=$inputs.inputs key=name item=input}
$('#{$name}-operator').change(function (e) { // Operator selector
$('#{$name}-operator').change(function (e) {
var $this = $(this); var $this = $(this);
console.log('{$name}-operator changed by ' + $this.val()); ruleToSave['operators']['{$name nofilter}'] = $this.val();
ruleToSave['operators']['{$name nofilter}'] = $this.val(); });
}); // Value input
$('#{$name}-value').change(function (e) { $('#{$name}-value').change(function (e) {
var $this = $(this); var $this = $(this);
console.log('{$name}-value changed by ' + $this); ruleToSave['values']['{$name nofilter}'] = $this.val();
ruleToSave['values']['{$name nofilter}'] = $this.val(); });
}); {/foreach}
{/foreach}
console.log('will save ');
console.log(ruleToSave);
{literal}}{/literal} {literal}}{/literal}
onInputsChange(); onInputsChange();
</script> </script>